import sys
sys.path.append("resource/")
from config.constant import *
from data.json_wr import *
from PyQt5.QtWidgets import QApplication, QWidget,QMessageBox
from gui.windows.startWin import Ui_StartWindow
from gui.dataWindow import DataWindow
from gui.profileWindow import ProfileWindow
from gui.analysisWindow import AnalysisWindow
from gui.cganWindow import CGANWindow
from gui.autoencoderWindow import AutoencoderWindow
import matplotlib.pyplot as plt

class StartWindow(QWidget, Ui_StartWindow):
    def __init__(self):
        super(StartWindow, self).__init__()
        self.setupUi(self)
        self.setFixedSize(431, 342)
        self.param_dict = json_read()
        self.get_proj_root()
        self.dataWindow=DataWindow(self)
        self.profileWindow=ProfileWindow(self)
        self.analysisWindow=AnalysisWindow(self)
        self.CGANWindow=CGANWindow(self)
        self.AutoencoderWindow=AutoencoderWindow(self)
        self.target_algorithm.addItems(ALGORITHM)
        self.attack_method.addItems(ATTACK_METHOD)
        self.leakage_model.addItems(LEAKAGE_MODEL[self.param_dict["target_algorithm"]])
        self.connect_slot()
        self.fresh()

    def get_proj_root(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        while not any(name in ('.git', '.idea', '.vscode') for name in os.listdir(current_dir)):
            parent_dir = os.path.dirname(current_dir)
            if parent_dir == current_dir:
                break
            current_dir = parent_dir
        self.param_dict['proj_root']=current_dir

    def prepare_data_method(self):
        self.hide()
        self.dataWindow.show()

    def profile_method(self):
        self.hide()
        self.profileWindow.show()

    def analysis_method(self):
        self.hide()
        self.analysisWindow.show()

    def connect_slot(self):
        self.target_algorithm.currentTextChanged.connect(self.set_target_algorithm)
        self.attack_method.currentTextChanged.connect(self.set_attack_method)
        self.leakage_model.currentTextChanged.connect(self.set_leakage_model)
        self.prepare_data.clicked.connect(self.prepare_data_method)
        self.profile.clicked.connect(self.profile_method)
        self.analysis.clicked.connect(self.analysis_method)

    def fresh(self):
        leakage_model_record=self.param_dict["leakage_model"]
        self.target_algorithm.setCurrentText(self.param_dict["target_algorithm"])
        self.attack_method.setCurrentText(self.param_dict["attack_method"])
        self.param_dict["leakage_model"]=leakage_model_record
        self.leakage_model.setCurrentText(self.param_dict["leakage_model"])
        self.set_attack_method(self.param_dict["attack_method"])

    def set_target_algorithm(self, target_algorithm):
        self.param_dict["target_algorithm"]=target_algorithm
        self.leakage_model.clear()
        self.leakage_model.addItems(LEAKAGE_MODEL[target_algorithm])

    def set_attack_method(self, attack_method):
        self.param_dict["attack_method"]=attack_method
        if attack_method in ATTACK_METHOD_PROFILE:
            self.profile.show()
        else:
            self.profile.hide()
        self.profileWindow.reset_attacker(attack_method)
        self.analysisWindow.reset_attacker(attack_method)

    def set_leakage_model(self,leakage_model):
        self.param_dict["leakage_model"] = leakage_model

    def closeEvent(self, a0):
        result = QMessageBox.question(QWidget(), "保存参数", "是否保存当前参数？",QMessageBox.Yes | QMessageBox.No)
        if result==QMessageBox.Yes:
            json_write(self.param_dict)


if __name__ == '__main__':
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    app = QApplication(sys.argv)
    start_win = StartWindow()
    start_win.show()
    sys.exit(app.exec_())